package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") EmployeeQueryObject qo){
        // List<Employee> employees = employeeService.listAll();
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        // System.out.println(pageInfo.getPages());
        model.addAttribute("pageInfo",pageInfo);

        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments",departments);

        return "/employee/list";
    }

    @RequestMapping("/input")
    public String input(Long id,Model model){


        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments",departments);

        List<Role> roles = roleService.listAll();
        model.addAttribute("roles",roles);


        if (id != null) {
            /* 查看是否是编辑页面,过滤掉修改密码功能 */


            Employee employee = employeeService.get(id);
                model.addAttribute("employee",employee);
        }

        return "/employee/input";
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee,Long[] ids){
        try {
            if (employee.getId() != null) {
                employeeService.update(employee,ids);
            }else {
                employeeService.save(employee,ids);
            }
            return new JsonResult();
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult(false,"操作失败");
        }


    }


    @RequestMapping("/checkName")
    @ResponseBody
    public Map<String,Boolean> checkName(String name,Long id){  // valid:true
        HashMap<String, Boolean> map = new HashMap<>();
        // 判断是否是编辑操作
        if(id == null){
            // 如果不是编辑操作
            // 先去数据库获取有没有当前用户名用户
            Employee emp = employeeService.selectByName(name);
            // 返回结果给页面
            map.put("valid",emp==null);
        }else {
            // 如果是编辑操作
            // 先通过传过来的id 判断当前用户名 和 页面传过来的用户名是否一致
            Employee employee = employeeService.get(id);
            if (name.equals(employee.getName())) {
                // 如果一致 表示没有做用户名更改,则返回 true
                map.put("valid", true);
            } else {
                // 如果不一致 表示需要更改用户名
                // 先拿更改之后的用户名去数据库查找,看看有没有该用户名
                Employee emp = employeeService.selectByName(name);
                // 有或者没有 都返回给页面
                map.put("valid", emp == null);
            }
        }
        return map;
    }

    @RequestMapping("/delete")
    public String delete(Long id){
        employeeService.delete(id);
        return "redirect:/employee/list.do";
    }

    @RequestMapping("/deleteCheckedList")
    @ResponseBody
    public JsonResult deleteCheckedList(Long[] ids){

        try {
            employeeService.deleteCheckedList(ids);
            return new JsonResult();
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult(false,"删除失败");
        }

    }

}
